package com.bdbit.ChargingStation.config;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MqttCallbackHandler implements MqttCallback {

    @Override
    public void connectionLost(Throwable cause) {
        log.error("MQTT连接丢失，原因:", cause);
        log.info("尝试重新连接...");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        // 可选：记录消息发送完成
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
    String payload = new String(message.getPayload());
    // 过滤掉服务端发出的消息日志
        if (!payload.startsWith("[SERVER]")) {
            log.info("收到设备消息 | 主题: {}, 内容: {}", topic, payload);
        }
    }
}